package com.javabasic.algorithm.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * n叉树的前序遍历
 */
public class N_aryTreePreorderTraversal {

    public List<Integer> preorder(Node root) {
        List<Integer> result = new ArrayList<>();
        executorPreOrder(root,result);
        return  result;
    }

    public void executorPreOrder(Node root,List<Integer> result) {
        if (root != null) {
            result.add(root.val); // 先放自己的值
            int len = root.children.size();
            for (int i = 0; i < len; i++) {
                executorPreOrder(root.children.get(i),result); // 再放子树
            }
        }
    }
}
/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val,List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/